package com.example.forum.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.forum.MyQuery;
import com.example.forum.entity.*;
import com.example.forum.mapper.*;
import com.example.forum.service.ITThemereplyService;
import com.example.forum.vo.PageDto;
import com.example.forum.vo.ReplyDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author why
 * @since 2020-04-22
 */
@Service
public class TThemereplyServiceImpl extends ServiceImpl<TThemereplyMapper, TThemereply> implements ITThemereplyService {

    @Autowired
    TThemereplyMapper tThemereplyMapper;

    @Autowired
    TThemeMapper tThemeMapper;

    @Autowired
    TBlockMapper blockMapper;

    @Autowired
    TAuditpolicyMapper auditpolicyMapper;

    @Autowired
    TBlockareaMapper blockareaMapper;

    @Autowired
    TSensitivewordsMapper sensitivewordsMapper;


    @Override
    public PageDto getThemesReplyByThemeId(Integer pageIndex, Integer pageSize, String themeId,String userId,boolean isDesc) {
        HashMap<Object, Object> parms = new HashMap<>();
        parms.put("pageIndex",pageIndex);
        parms.put("pageSize",pageSize);
        parms.put("themeId",themeId);
        parms.put("userId",userId);
        parms.put("isDesc",isDesc);
        List<ReplyDto> list = tThemereplyMapper.getThemesReplyByThemeId(parms);
        Long totle = tThemereplyMapper.getThemesReplyCountByThemeId(parms);
        return new PageDto(totle,list);
    }

    @Transactional
    @Override
    public void createThemeReply(ThemereplyModel themereplyModel, String id) {

        TThemereply tThemereply = new TThemereply();
        tThemereply.setTopicid(themereplyModel.getTopicId());
        tThemereply.setType(themereplyModel.getType());
        tThemereply.setCreatoruserid(id);
        tThemereply.setIsdeleted(false);

        //获取审核规则 待优化
//        TTheme tTheme = tThemeMapper.selectById(themereplyModel.getTopicId());
//        TBlockarea tBlockarea = blockareaMapper.selectById(tTheme.getBlockareaid());
//        TBlock tBlock = blockMapper.selectById(tBlockarea.getBlockid());
//        TAuditpolicy tAuditpolicy = auditpolicyMapper.selectById(tBlock.getPolicyid());
        TAuditpolicy tAuditpolicy = auditpolicyMapper.selectAuditByThemeId(themereplyModel.getTopicId());
        if (tAuditpolicy.getPolicyname().equals("敏感词过滤后展现")) {
            //查询敏感词
            List<String> terms = sensitivewordsMapper.selectList(new MyQuery<TSensitivewords>().select("terms").eq("isrelease", 1))
                    .stream().map(TSensitivewords::getTerms).collect(Collectors.toList());
            //内容过滤
            for (String term : terms) {
                //如果输入的文本包含敏感词 用*代替
                if (themereplyModel.getContent().contains(term)) {
                    char[] chars = term.toCharArray();
                    for (char str : chars) {
                        themereplyModel.setContent(themereplyModel.getContent().replace(str,'*'));
//                        tThemereply.setContent( themereplyModel.getContent().replace(str,'*') );
                    }
                }
            }
            tThemereply.setContent(themereplyModel.getContent());
            tThemereply.setStatus(2);//审核通过
        }else if(tAuditpolicy.getPolicyname().equals("人工审核后展现")){
            tThemereply.setContent(themereplyModel.getContent());
            tThemereply.setStatus(2);//默认审核通过
        }
        long time = new Date().getTime();
        tThemereply.setAuditdate(new Date(time));
        tThemereply.setDeletetype(0);

        tThemereplyMapper.insert(tThemereply);

    }
}
